package com.example.tourpal.widget;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class SpotExpandableAdapter extends BaseExpandableListAdapter {

	private ExpandableList spotExpandable = new ExpandableList();
	private Context context;

	private ExpandableListView spotList;

	private TextView getTextView(int type) {
		AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
				ViewGroup.LayoutParams.MATCH_PARENT,
				ViewGroup.LayoutParams.WRAP_CONTENT);
		TextView textView = new TextView(context);
		textView.setLayoutParams(lp);
		textView.setGravity(Gravity.CENTER_VERTICAL);
		textView.setPadding(64, 8, 0, 8);
		if (type == 0) {
			textView.setTextSize(24);
			textView.setTextColor(Color.GRAY);
		} else {
			textView.setTextSize(30);
			textView.setBackgroundColor(Color.WHITE);
			textView.setTextColor(Color.GRAY);
		}
		return textView;

	}

	public SpotExpandableAdapter(Context context, ExpandableList expandableList) {
		this.context = context;
		spotExpandable = expandableList;
	}

	@Override
	public Object getChild(int pIndex, int cIndex) {
		// TODO Auto-generated method stub
		return spotExpandable.childList.get(pIndex).get(cIndex);
	}

	@Override
	public long getChildId(int arg0, int arg1) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getChildView(int pIndex, int cIndex, boolean isLastChild,
			View convertView, ViewGroup parent) {
		LinearLayout linearLayout = new LinearLayout(context);
		linearLayout.setOrientation(LinearLayout.VERTICAL);
		TextView child = getTextView(1);
		child.setText(getChild(pIndex, cIndex).toString());
		linearLayout.addView(child);
		return linearLayout;
	}

	@Override
	public int getChildrenCount(int index) {
		// TODO Auto-generated method stub
		return spotExpandable.childList.get(index).size();
	}

	@Override
	public Object getGroup(int index) {
		// TODO Auto-generated method stub
		return spotExpandable.groupList.get(index);
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return spotExpandable.groupList.size();
	}

	@Override
	public long getGroupId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getGroupView(int index, boolean isExpanded, View convertView,
			ViewGroup parent) {
		LinearLayout linearLayout = new LinearLayout(context);
		linearLayout.setOrientation(LinearLayout.VERTICAL);
		TextView child = getTextView(0);
		child.setText(getGroup(index).toString());
		linearLayout.addView(child);
		return linearLayout;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isChildSelectable(int arg0, int arg1) {
		// TODO Auto-generated method stub
		return true;
	}

	public ExpandableListView getSpotList() {
		return spotList;
	}

	public void setSpotList(ExpandableListView spotList) {
		this.spotList = spotList;
	}

	@Override
	public void onGroupExpanded(int groupPosition) {
		Log.e("YLQ", "groupPosition=" + groupPosition);
		Log.e("YLQ", "getGroupCount()=" + getGroupCount());
		for (int i = 0; i < getGroupCount(); i++) {
			// ensure only one expanded Group exists at every time
			if (groupPosition != i && spotList.isGroupExpanded(groupPosition)) {
				spotList.collapseGroup(i);
			}
		}
	}

}
